package com.zl.service;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zl.common.enumType.Category;
import com.zl.common.enumType.Column;
import com.zl.dao.SelectorDao;
import com.zl.dao.SubscribeDao;
import com.zl.dao.SubscriptionDao;
import com.zl.entity.Area;
import com.zl.entity.QueryEntity;
import com.zl.entity.SubscribeEntity;
import com.zl.entity.SubscriptionEntity;

@Service
public class SubscribeService {
	@Autowired
	private SubscribeDao subscribeDao;
	@Autowired
	private SelectorDao selectorDao;
	@Autowired
	private SubscriptionDao subscriptionDao;
	public List<SubscribeEntity> getSubscribeByOpenId(String openid){
		return subscribeDao.getSubscribeByOpenId(openid);
	}
	
	public List<SubscribeEntity> getSubEntity(String openid){
		List<SubscribeEntity> list = subscribeDao.getSubscribeByOpenId(openid);
		for(SubscribeEntity subscribe:list){
			if(StringUtils.isNotBlank(subscribe.getCategory())){
				String[] categorys = subscribe.getCategory().split(",");
				StringBuffer cateSb = new StringBuffer();
				int i = 1;
				for(String category:categorys){
					String categoryStr = Category.getDylb(category).getName();
					cateSb.append(categoryStr);
					if(i!=categorys.length){
						cateSb.append(",");
					}
					i++;
				}
				subscribe.setCategory(cateSb.toString());
			}else{
				subscribe.setCategory("全部类别");
			}
			
			if(StringUtils.isNotBlank(subscribe.getColumn())){
				String[] columns = subscribe.getColumn().split(",");
				StringBuffer columnSb = new StringBuffer();
				int i = 1;
				for(String column:columns){
					String columnStr = Column.getGglb(column).getName();
					columnSb.append(columnStr);
					if(i!=columns.length){
						columnSb.append(",");
					}
					i++;
				}
				subscribe.setColumn(columnSb.toString());
			}else{
				subscribe.setColumn("全部栏目");
			}
			
			if(StringUtils.isNotBlank(subscribe.getArea())){
				String placeType = subscribe.getAreaType();
				String place = subscribe.getArea();
				Area area = null;
				if("1".equals(placeType)){
					area = selectorDao.getProvinceById(place);
				}else{
					area = selectorDao.getCityById(place);
				}
				subscribe.setArea(area.getName());
			}
			if(StringUtils.isNotBlank(subscribe.getKeywords())){
				String[] keywords = subscribe.getKeywords().split(" ");
				List<SubscriptionEntity> subscriptionEntity = subscriptionDao.selectByKey(keywords[0].trim());
				if(subscriptionEntity.size()>0){
					subscribe.setNum(Integer.toString(subscriptionEntity.get(0).getCount()));
				}else{
					subscribe.setNum("0");
				}
				
			}
			
		}
		return list;
	}
	
	public void insertSubscribe(SubscribeEntity subscribeEntity){
		String[] keywords = subscribeEntity.getKeywords().split(" ");
		List<SubscriptionEntity> subscriptionEntity = subscriptionDao.selectByKey(keywords[0].trim());
		if(subscriptionEntity.size()>0){
			subscriptionDao.updateSubscription(keywords[0].trim());
		}else{
			SubscriptionEntity subscription = new SubscriptionEntity();
			subscription.setZj(UUID.randomUUID().toString());
			subscription.setKey(keywords[0].trim());
			subscriptionDao.insertSubscription(subscription);
		}
		subscribeDao.insertSubscribe(subscribeEntity);
	}
	
	public void updateSubscribe(SubscribeEntity subscribeEntity){
		subscribeDao.updateSubscribe(subscribeEntity);
	}
	
	public void deleteById(String id){
		subscribeDao.deleteById(id);
	}
	
	public SubscribeEntity selectById(String id){
		return subscribeDao.selectById(id);
	}
}
